// ActionScript file.
import flash.events.DataEvent;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.FileReference;
import flash.net.FileReferenceList;

import mx.collections.ArrayCollection;


/* goods id*/
public var goodsid:Number = 0;


// 下拉列表的绑定变量
[Bindable] public var goodslist:ArrayCollection = new ArrayCollection([{label:"请选择商品", data:0}]);
/**/
[Bindable] public var photoslist:ArrayCollection = new ArrayCollection();


// 取商品列表的程序
private var URLGoodsPhotoRPC:String = 'getgoodsphotos.php';
/* 删除图片 */
private var URLDelPhotoRPC:String = 'delphoto.php';


//private var fileRef:FileReference = new FileReference();
private var fileList:FileReferenceList = new FileReferenceList();
private var imgfilter:FileFilter = new FileFilter("Image", "*.png;*.jpg;*jpeg;*JPEG");

/* 进度条 */
private var mp:myprogress;

/* 文件总数 */
private var countFile:Number = 0;
private var countDo:Number = 0;

/* 存放选择的文件 */
private var selectedFileArray:Array;



/**
 * 初始化
 * 
 * 1、取属于自制成品的商品名称
 * 
 */
public function init():void{
	this.goodsid = Application.application.parameters["gid"];
	//this.SendHttpService(this.URLGoodsPhotoRPC, {gid:this.goodsid}, this.getGoodsPhotoRPCResult, 'Goods Photos');
	this.getGoodsPhotos();
	fileList.addEventListener(Event.ACTIVATE, fileActivate);
    fileList.addEventListener(Event.SELECT, fileSelect);
    fileList.addEventListener(Event.DEACTIVATE, fileDeactivate);
    fileList.addEventListener(Event.CANCEL, fileCancel);
    

    mp = new myprogress();
    addChild(mp);
    this.mp.visible = false;
}

/**
 * 取此商品的图片
 * 
 */
private function getGoodsPhotos():void{
	this.SendHttpService(this.URLGoodsPhotoRPC, {gid:this.goodsid}, this.getGoodsPhotoRPCResult, 'Goods Photos');
}

/**
 * 取商品图片的回调
 * 
 */
private function getGoodsPhotoRPCResult(e:ResultEvent):void{
	//trace(e.result);
	this.photoslist.removeAll();
	for each (var s:XML in e.result.photo){
		photoslist.addItem({url: s.thumb, id: s.img_id});
	}
}


/**
 * 删除图片
 * 
 */
public function delMyMaked(pid:Object):void{
	trace(pid);
	this.SendHttpService(this.URLDelPhotoRPC, {id:pid},this.delPhotoRPCResult, 'delete photo');
}

/**
 * 删除指定照片
 */
private function delPhotoRPCResult(e:ResultEvent):void{
	trace(e.result);
	this.getGoodsPhotos();
}

/**
 * 选择照片
 * 
 */
private function SelectPhoto():void{
	fileList.browse([imgfilter]);
	

}

private function fileActivate(e:Event):void{

}


/**
 * 文件选择
 *
 */
private function fileSelect(e:Event):void{
    this.mp.visible = true;
    
    var files:FileReferenceList = FileReferenceList(e.target);
    selectedFileArray = files.fileList;
    this.countFile = this.selectedFileArray.length;
    this.countDo = 0;
    this.uploadFileSingle(this.countDo);
    
}

private function fileDeactivate(e:Event):void{

}

private function fileCancel(e:Event):void{

}

/**
 * FileReference的complete事件
 */
private function completeHandler(e:Event):void{
       
}

/* UPLOAD_COMPLETE_DATA */
private function uploadCompleteDataHandler(e:DataEvent):void{
	//Alert.show(e.data);
	//var str:String = (this.countDo+1).toString();
	//if(e.data == '1'){
		this.countDo++;
		//Alert.show(this.countDo.toString());
    	if(this.countDo < this.selectedFileArray.length){
    		this.uploadFileSingle(this.countDo);
    	}
    	this.countFile--;
    	//Alert.show(this.countFile.toString());
    	if(this.countFile==0){
    		//Alert.show('成功上传'+str+'个文件.','提示');
    		this.mp.visible = false;
    		this.getGoodsPhotos();
    	}
 	//}else if(e.data == '0'){
 		//Alert.show('系统错误，已成功上传'+str+'个文件.','系统错误');
 	//}
}

/**
 * 文件IO错误
 */
private function fileErrorHandle(e:ErrorEvent):void{
	//trace(e.toString());
	Alert.show("系统错误！","错误");
}



private function progressHandle(e:ProgressEvent):void{
	var prog:Number = e.bytesLoaded/e.bytesTotal;
	this.mp.goprocess(prog);
}

/* fileStatus */
private function fileStatus(e:HTTPStatusEvent):void{
	//trace(e.toString());
}

private function uploadFileSingle(cou:Number):void{
	var request:URLRequest = new URLRequest("upload.php");
    var variables:URLVariables = new URLVariables();
    variables.gid = this.goodsid;
    request.data=variables;
	request.method=URLRequestMethod.POST;
	
	//this.countFile = this.selectedFileArray.length;
    //for(var i:uint=0; i<this.selectedFileArray.length; i++){
    	var file:FileReference;
    	file = FileReference(this.selectedFileArray[cou]);
    	//Alert.show(file.name);
        file.addEventListener(Event.COMPLETE, completeHandler);
        file.addEventListener(IOErrorEvent.IO_ERROR, fileErrorHandle);
        file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
        file.addEventListener(HTTPStatusEvent.HTTP_STATUS, fileStatus);
        file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);

        try{
        	file.upload(request);
        }catch (error:Error){
            trace("Unable to upload files.");
            //trace(error.message);
            Alert.show('上传失败','错误');
        }
    //}
}
